package views;
/**
 * EDCNavigator
 * Viewer for current list of DTD files
 */
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import ui.EDC;
import utils.EDCResource;
import utils.EDCUtils;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

public class EDCNavigator extends EDCDefaultViewer
implements TreeSelectionListener, ActionListener, ItemListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static EDCNavigator current;
	private JTree tree;
	private static boolean DEBUG = false;
	private static DefaultMutableTreeNode top;
	// Optionally play with line styles. Possible values are
	// "Angled" (the default), "Horizontal", and "None".
	private static boolean playWithLineStyle = false;
	private static String lineStyle = "Horizontal";

	// popup menu
	private JPopupMenu popup;

	public EDCNavigator() {
		setTitle("Navigator");
//		super(new GridLayout(1, 0));
		getContentPane().setLayout(new GridLayout(1, 0));

		// Create the nodes.
		top = new DefaultMutableTreeNode("DTD files");
//		createNodes(top,EDCResource.dtdFolderPath);

		current = this;
		
		// Create a tree that allows one selection at a time.
		tree = new JTree(top);
		tree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);

		// Listen for when the selection changes.
		tree.addTreeSelectionListener(this);

		if (playWithLineStyle) {
			System.out.println("line style = " + lineStyle);
			tree.putClientProperty("JTree.lineStyle", lineStyle);
		}

		// Create the scroll pane and add the tree to it.
		JScrollPane treeView = new JScrollPane(tree);
		Dimension minimumSize = new Dimension(100, 50);
		treeView.setMinimumSize(minimumSize);
		getContentPane().add(treeView);
	}

	/** Required by TreeSelectionListener interface. */
	public void valueChanged(TreeSelectionEvent e) {
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
		.getLastSelectedPathComponent();

		popup = new JPopupMenu();
		JMenuItem menuItem;

		if (node == null)
			return;

		Object nodeInfo = node.getUserObject();
		if (node.isLeaf()) {
			if (DEBUG) {
//				System.out.print();
			}
			//...where the GUI is constructed:
			//Create the popup menu.

			//a submenu
			JMenuItem submenu;
			submenu = new JMenu("View with");
			submenu.setMnemonic(KeyEvent.VK_S);
			submenu.addActionListener(this);
			popup.add(submenu);

			menuItem = new JMenuItem(EDCResource.TEXT_VIEWER);
			menuItem.setAccelerator(KeyStroke.getKeyStroke(
			        KeyEvent.VK_2, ActionEvent.ALT_MASK));
			menuItem.addActionListener(this);
			submenu.add(menuItem);

//			menuItem = new JMenuItem(EDCResource.DTD_VIEWER);
//			menuItem.addActionListener(this);
//			submenu.add(menuItem);
			
			popup.addSeparator();
			
			menuItem = new JMenuItem(EDCResource.ANALYZE_FILE);
			menuItem.addActionListener(this);
			popup.add(menuItem);
		} else {

			menuItem = new JMenuItem(EDCResource.ANALYZE_FOLDER);
			menuItem.addActionListener(this);
			popup.add(menuItem);
		}
		
		//Add listener to components that can bring up popup menus.
		MouseListener popupListener = new PopupListener();
		tree.addMouseListener(popupListener);
		
		if (DEBUG) {
			System.out.println(nodeInfo.toString());
		}
	}

	private class DTDNode {
		public String dtdName;

		public DTDNode(String name, String other) {
			dtdName = name;
		}

		public String toString() {
			return dtdName;
		}
	}

	public void createNodes(DefaultMutableTreeNode top, String folderPath) {

		DefaultMutableTreeNode dtd = null;

		System.out.println(folderPath);
		File[] files = EDCUtils.getFiles(folderPath);
		if(files!=null && files.length > 0) {
			for(int i=0; i<files.length; i++) {
				dtd = new DefaultMutableTreeNode(new DTDNode(
						files[i].getName(),
				""));
				top.add(dtd);
			}
		}
		else {
			System.out.println("directory empty!");
		}
	}
	
	
	class PopupListener extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			maybeShowPopup(e);
		}

		public void mouseReleased(MouseEvent e) {
			maybeShowPopup(e);
		}

		private void maybeShowPopup(MouseEvent e) {
			if (e.isPopupTrigger()) {
				popup.show(e.getComponent(),
						e.getX(), e.getY());
			}
		}
	}

	public void actionPerformed(ActionEvent e) {
		JMenuItem source = (JMenuItem)(e.getSource());

		if(source.getText()==EDCResource.TEXT_VIEWER) {
			EDCResource.TEXT_VIEW_REQUIRED = true;
			EDCResource.DTD_VIEW_REQUIRED = false;
			EDCResource.ANALYZE_FILE_REQUIRED = false;
			EDCResource.ANALYZE_FOLDER_REQUIRED = false;
			EDCResource.ADD_DTD_REQUIRED = false;
			EDCResource.MANAGER_VIEW_REQUIRED = false;
			System.out.println(source.getText()+EDCResource.TEXT_VIEW_REQUIRED);
			System.out.println("Noeud : "+tree.getSelectionPath().getPathComponent(1).toString());
			String selectedDTD = tree.getSelectionPath().getPathComponent(1).toString();
			EDC.getCurrent().setFilename(selectedDTD);
			EDC.getCurrent().repaint();
			
		}
//		else if(source.getText()==EDCResource.DTD_VIEWER) {
//			EDCResource.TEXT_VIEW_REQUIRED = false;
//			EDCResource.DTD_VIEW_REQUIRED = true;
//			EDCResource.ANALYZE_FILE_REQUIRED = false;
//			EDCResource.ANALYZE_FOLDER_REQUIRED = false;
//			EDCResource.ADD_DTD_REQUIRED = false;
//			EDCResource.MANAGER_VIEW_REQUIRED = false;
//			System.out.println(source.getText()+EDCResource.DTD_VIEW_REQUIRED);
//			EDC.getCurrent().repaint();
//		}
		else if(source.getText()==EDCResource.ANALYZE_FILE) {
			EDCResource.TEXT_VIEW_REQUIRED = false;
			EDCResource.DTD_VIEW_REQUIRED = false;
			EDCResource.ANALYZE_FILE_REQUIRED = true;
			EDCResource.ANALYZE_FOLDER_REQUIRED = false;
			EDCResource.ADD_DTD_REQUIRED = false;
			EDCResource.MANAGER_VIEW_REQUIRED = false;
			System.out.println(source.getText()+EDCResource.ANALYZE_FILE_REQUIRED);
			System.out.println("Noeud : "+tree.getSelectionPath().getPathComponent(1).toString());
			String selectedDTD = tree.getSelectionPath().getPathComponent(1).toString();
			EDC.getCurrent().setFilename(selectedDTD);
			EDC.getCurrent().repaint();
		}
		else if (source.getText()==EDCResource.ANALYZE_FOLDER) {
			EDCResource.TEXT_VIEW_REQUIRED = false;
			EDCResource.DTD_VIEW_REQUIRED = false;
			EDCResource.ANALYZE_FILE_REQUIRED = false;
			EDCResource.ANALYZE_FOLDER_REQUIRED = true;
			EDCResource.ADD_DTD_REQUIRED = false;
			EDCResource.MANAGER_VIEW_REQUIRED = false;
			System.out.println(source.getText()+EDCResource.ANALYZE_FOLDER_REQUIRED);
			EDC.getCurrent().repaint();
		}
		else {
			System.out.println(source.getText());
		}

	}

	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		JMenuItem source = (JMenuItem)(e.getSource());

//		String s = "Item event detected."
//		+ ((e.getStateChange() == ItemEvent.SELECTED) ?
//		"selected":"unselected");

		if(e.getStateChange() == ItemEvent.SELECTED) {

		}
	}
	
	public static EDCNavigator getCurrent(){
		return current;
	}
	
	public DefaultMutableTreeNode getRoot(){
		return top;
	}
	
	public JTree getTree(){
		return tree;
	}
	
	public void resetTree(){
		top.removeAllChildren();
	}

}

